import logging

from flask import Blueprint
from flask import jsonify
from flask import request

from airflow.patsnap.util.ding import Ding
from airflow.models.idata_alert import Alert
from airflow.www.api import exception_handle
from airflow.patsnap.service import alert_service

log = logging.getLogger(__name__)

alert_bp = Blueprint('api_speech_alert', __name__)


@alert_bp.route('/alerts/<int:alert_id>', methods=['GET'])
@exception_handle
def get_alert(alert_id):
    alert = Alert.get_alert(alert_id)
    if alert:
        return jsonify(code=0, message='ok', data=alert.to_json())
    else:
        return jsonify(code=404, message='task not found', data=None)


@alert_bp.route('/alerts/<int:alert_id>', methods=['DELETE'])
@exception_handle
def delete_alert(alert_id):
    alert = Alert(id=alert_id)
    alert.delete()
    return jsonify(code=0, message='ok', data=alert.to_json())


@alert_bp.route('/alerts/category', methods=['GET'])
@exception_handle
def get_alert_category():
    data = [
        {
            'label': '钉钉',
            'value': 0
        }
    ]
    return jsonify(code=0, message='ok', data=data)


@alert_bp.route('/alerts/options', methods=['GET'])
@exception_handle
def query_options():
    project_id = request.args.get('project_id', None)
    category = request.args.get('category', None)
    if category:
        category = int(category)
    rs = Alert.query_options(project_id=project_id, category=category)
    return jsonify(code=0, message='ok', data=rs)


@alert_bp.route('/alerts', methods=['GET'])
@exception_handle
def query_page():
    project_id = request.args.get('project_id', None)
    if not project_id:
        return jsonify(code=0, message='project_id is null', data=None)
    category = request.args.get('category', None)
    if category:
        category = int(category)
    page = request.args.get('page', 1)
    page = int(page)
    page_size = request.args.get('page_size', 10)
    page_size = int(page_size)
    rs = Alert.query_page(project_id=project_id, category=category, page=page, page_size=page_size)
    return jsonify(code=0, message='ok', data=rs)


@alert_bp.route('/alerts/<int:alert_id>', methods=['PUT'])
@exception_handle
def update_alert(alert_id):
    data = request.get_json()
    if 'id' not in data:
        data['id'] = alert_id
    if 'category_name' in data:
        data.pop('category_name')
    alert = Alert(**data)
    alert.update()
    return jsonify(code=0, message='ok', data=alert.to_json())


@alert_bp.route('/alerts', methods=['POST'])
@exception_handle
def insert_alert():
    data = request.get_json()
    alert = Alert(**data)
    alert.insert()
    return jsonify(code=0, message='ok', data=alert.to_json())


@alert_bp.route('/alerts/<int:alert_id>/send', methods=['POST'])
@exception_handle
def send_alert(alert_id):
    alert = Alert.get_alert(alert_id)
    if alert is None:
        return jsonify(code=404, message='alert not found', data=None)
    if alert and alert.category == 0:
        Ding(alert.content, 'This is test alert').send()
        return jsonify(code=0, message='ok', data=alert.to_json())


@alert_bp.route('/alerts/datafactory/send', methods=['POST'])
@exception_handle
def send_datafactory_alert():
    data = request.get_json()
    task_id = data.get('task_id', None)
    task_name = data.get('task_name', None)
    project_name = data.get('project_name', None)
    delay = data.get('delay', None)
    interval = data.get('interval', None)
    if task_id and delay and interval:
        msg = "任务务延时:{}分钟内平均时延:{}".format(str(interval), str(delay))
        alert_service.send_task_alerts(task_id=task_id, condition=2, msg=msg, task_name=task_name, project_name=project_name)
        return jsonify(code=0, message='ok', data=None)